package main

import (
	"errors"
	"fmt"
	"time"

	"gitee.com/rainsuperman/simple-redis-go/srg"
	"github.com/redis/go-redis/v9"
)

func main() {

	fmt.Println("hello world")

	rds := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       6,  // use default DB
	})
	srg.SetRds(rds) //设置redis连接
	srg.StartSrg()

	srg.RegisterFlowWorker(srg.SrgFlowWorker{
		FlowWork: func(flowWork srg.FlowWork) (string, error) {
			fmt.Println("localOrder执行")
			return "", nil
		},
		FlowRollBackWork: func(flowWork srg.FlowWork) (string, error) {
			fmt.Println("localOrder回滚")
			return "", nil
		},
		MatchFlowType: func(flowType string) bool {
			return flowType == "localOrder"
		},
	})

	srg.RegisterFlowWorker(srg.SrgFlowWorker{
		MatchFlowType: func(flowType string) bool {
			return flowType == "thirdOrder"
		},
		FlowWork: func(flowWork srg.FlowWork) (string, error) {
			fmt.Println("thirdOrder执行")
			return "", errors.New("thirdOrder error")
		},
		FlowRollBackWork: func(flowWork srg.FlowWork) (string, error) {
			fmt.Println("thirdOrder回滚")
			return "", nil
		},
	})

	srg.RegisterFlowWorker(srg.SrgFlowWorker{
		MatchFlowType: func(flowType string) bool {
			return flowType == "thirdPay"
		},
		FlowWork: func(flowWork srg.FlowWork) (string, error) {
			fmt.Println("thirdPay执行")
			return "OK", errors.New("thirdPay error")
		},
		FlowRollBackWork: func(flowWork srg.FlowWork) (string, error) {
			fmt.Println("thirdPay回滚")
			return "OK", nil
		},
	})
	time.Sleep(time.Second * 3)
	srg.FlowWorkAdd("10", time.Now().UnixMilli()+1000*5, "localOrder", "thirdOrder", "thirdPay")
	time.Sleep(time.Hour * 5)

}
